package com.glp.work.flow.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.glp.work.flow.constant.Constants;
import com.glp.work.flow.constant.MessageTypeEnum;
import com.glp.work.flow.dto.SendEmailSituationDTO;
import com.glp.work.flow.dto.WfPlatformDefDTO;
import com.glp.work.flow.mapper.WfPlatformDefMapper;
import com.glp.work.flow.mapper.WfPlatformDefModleMapper;
import com.glp.work.flow.pojo.WfPlatformDef;
import com.glp.work.flow.pojo.WfPlatformDefModle;
import com.glp.work.flow.service.WfPlatformDefService;
import com.glp.work.flow.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Slf4j
public class WfPlatformDefServiceImpl implements WfPlatformDefService {
    @Autowired
    private WfPlatformDefMapper wfPlatformDefMapper;

    @Autowired
    private WfPlatformDefModleMapper wfPlatformDefModleMapper;

    @Override
    public void authorization(List<String> platformIds, String mqKey, String messageType, String modelKey, String userNo) {
        //删除已存在授权
        log.info("删除用户子表信息，主表ID：{}", modelKey);
        Example example3 = new Example(WfPlatformDefModle.class);
        example3.createCriteria().andEqualTo("modelKey", modelKey);
        wfPlatformDefModleMapper.deleteByExample(example3);
        if (!CollectionUtils.isEmpty(platformIds)) {
            for (String platformId : platformIds) {
                if (!StringUtils.isEmpty(platformId)) {
                    WfPlatformDefModle defModel = new WfPlatformDefModle();
                    defModel.setModelKey(modelKey);
                    defModel.setMqKey(mqKey);
                    defModel.setAppId(platformId);
                    defModel.setCreateUser(userNo);
                    defModel.setCreateTime(DateUtils.getCurrent());
                    defModel.setUpdateUser(userNo);
                    defModel.setUpdateTime(DateUtils.getCurrent());
                    log.info("新增平台授权：{}", defModel);
                    //授权
                    wfPlatformDefModleMapper.insertSelective(defModel);
                }
            }
        }
    }

    @Override
    public List<WfPlatformDefDTO> wpfDefinsertFormCase() {
        List<WfPlatformDefDTO> wfPlatformDefDTOS = wfPlatformDefMapper.wpfDefinsertFormCase();
        return wfPlatformDefDTOS;
    }

    @Override
    public List<WfPlatformDef> findAuthorizationByKey(String key) {
        return wfPlatformDefMapper.findAuthorizationByKey(key);
    }

    @Override
    public List<String> findActRuKey(String key) {

        return wfPlatformDefMapper.findActRuKey(key);
    }

    @Override
    public WfPlatformDefModle validateAppIdRole(String appId, String procDefKey) {
        Example example = new Example(WfPlatformDefModle.class);
        example.createCriteria()
                .andEqualTo("appId", appId)
                .andEqualTo("modelKey", procDefKey);
        return wfPlatformDefModleMapper.selectOneByExample(example);

    }

    @Override
    public List<WfPlatformDef> selectByType() {
        Example example = new Example(WfPlatformDef.class);
        example.createCriteria()
                .andEqualTo("type", 0)
                .orEqualTo("type", 2);
        return wfPlatformDefMapper.selectByExample(example);
    }

    @Override
    public List<WfPlatformDef> wfPlatformDefList(String platformName, String type) {
        Example example = new Example(WfPlatformDef.class);
        if (!StringUtils.isEmpty(platformName)) {
            example.createCriteria().andLike("platformName", "%" + platformName + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            example.createCriteria().andEqualTo("type", type);
        }
        return wfPlatformDefMapper.selectByExample(example);
    }

    @Override
    public List<WfPlatformDefModle> wfPlantDefModelKey(String key) {
        Example example = new Example(WfPlatformDefModle.class);
        example.createCriteria().andEqualTo("modelKey", key);
        List<WfPlatformDefModle> wfPlatformDefModles = wfPlatformDefModleMapper.selectByExample(example);
        return wfPlatformDefModles;
    }

    @Override
    public PageInfo<SendEmailSituationDTO> getSendEmailSituation(int pageNum, int pageSize) {
        return PageMethod.startPage(
                pageNum, pageSize).doSelectPageInfo(() -> wfPlatformDefMapper.getSendEmailSituation());
    }

    @Override
    public void updateSendEmailSituation(SendEmailSituationDTO sendEmailSituationDTO) {
        Example example = new Example(WfPlatformDef.class);
        example.createCriteria().andEqualTo("platformId", sendEmailSituationDTO.getPlatformId());
        WfPlatformDef wfPlatformDef = new WfPlatformDef();
        wfPlatformDef.setPlatformId(sendEmailSituationDTO.getPlatformId());
        wfPlatformDef.setNotification(sendEmailSituationDTO.getNotification());
        wfPlatformDef.setUrlType(sendEmailSituationDTO.getUrlType());
        if (Constants.STATUS_2.equals(wfPlatformDef.getUrlType())) {
            wfPlatformDef.setUrl(sendEmailSituationDTO.getUrl());
        }
        wfPlatformDefMapper.updateByExampleSelective(wfPlatformDef, example);
    }
}
